import axios from 'axios'
import ca from "element-ui/src/locale/lang/ca";

export const getUrlParams = (path) => {
  let url = path || window.location.search;  //url中?之后的部分
  url = url.substring(1);    //去掉?
  let dataObj = {};
  if (url.indexOf('&') > -1) {
    url = url.split('&');
    for (let i = 0; i < url.length; i++) {
      let arr = url[i].split('=');
      dataObj[arr[0]] = arr[1];
    }
  } else {
    url = url.split('=');
    dataObj[url[0]] = url[1];
  }
  return dataObj;
}


export const initMenu = (router, store) => {
  if (store.state.menuList.length > 0) {
    return;
  }
  let userDe = JSON.parse(window.localStorage.getItem('user'));
  axios.get("/user/getMenuList?userId=" + userDe.id)
    .then(response => {
      if (response.data && response.data.code == 200) {
        var fmtRoutes = formatRoutes(response.data.data);
        router.addRoutes(fmtRoutes);
        store.commit('set_menuList', response.data.data);
      } else {
        router.push('/');
        console.log('请求菜单列表失败 ： ', err)
      }
    })
    .catch(err => {
      router.push('/');
      window.localStorage.removeItem('token');
      console.log('请求菜单列表异常 ： ', err)
    });
}
export const formatRoutes = (routes) => {
  let fmRoutes = [];
  routes.forEach(router => {
    let {
      name,
      url,
      path,
      requireAuth,
      component,
      // meta,
      icon,
      children
    } = router;
    if (children && children.length > 0 && children instanceof Array) {
      children = formatRoutes(children);
    }
    if (!requireAuth) {
      children = [
        {
          path: url,
          url: url,
          component(resolve) {
            require(['../views' + url + '.vue'], resolve)
          },
          name: "",
          icon: "",
          // meta: meta,
          children: []
        }
      ]
    }
    if(name == '日常管理') {
      children.forEach(ele => {
        ele.children.forEach(element => {
          let fmRouterC = {
            path: element.path,
            url: element.url,
            component(resolve) {
              if (element.component)
                require(['../views' + element.url + '.vue'], resolve)
            },
            name: element.name,
            icon: element.icon,
            // meta: meta,
            children: []
          };
          fmRoutes.push(fmRouterC);
        }) 
      })
      return;
    }
    if (name == '废气' || name == '废水' || name == '一般固废') {
      children.forEach(element => {
        if(element.name == '监测信息') {
          element.children.forEach(elej => {
            let fmRouterEleJ = {
              path: elej.path,
              url: elej.url,
              component(resolve) {
                if (elej.component)
                  require(['../views' + elej.url + '.vue'], resolve)
              },
              name: elej.name,
              icon: elej.icon,
              // meta: meta,
              children: []
            };
            fmRoutes.push(fmRouterEleJ);
          })
          return;
        }
        let fmRouterC = {
          path: element.path,
          url: element.url,
          component(resolve) {
            if (element.component)
              require(['../views' + element.url + '.vue'], resolve)
          },
          name: element.name,
          icon: element.icon,
          // meta: meta,
          children: []
        };
        fmRoutes.push(fmRouterC);
      })
      return;
    }
    let fmRouter = {
      path: path,
      url: url,
      component(resolve) {
        if (component)
          require(['../views/' + component + '.vue'], resolve)
      },
      name: name,
      icon: icon,
      // meta: meta,
      children: children
    };
    let param=null;
    if(path.indexOf('?')>-1){
      let urls=path.split('?'),p = urls[1];
      if (p) {
        param = getUrlParams('?'+p);
        fmRouter.path=urls[0];
      }
    }
    if(param){
      fmRouter.params=param;
    }
    fmRoutes.push(fmRouter);
  })
  return fmRoutes;
}
